package com.kxmall.web.controller.capacity;

import cn.dev33.satoken.annotation.SaIgnore;
import com.kxmall.capacity.domain.vo.KxCapacityVo;
import com.kxmall.common.core.controller.BaseAppController;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.utils.PageData;
import com.kxmall.web.controller.capacity.service.IKxCapacityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

/**
 * 旧版餐车柜子商品
 */

@Api(tags = "旧版餐车柜子商品")
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
@RequestMapping("/capacity")
public class KxCapacityController  extends BaseAppController {

    private final IKxCapacityService iKxCapacityService;

    /**
     * 获取餐车柜子状态
     */
    @SaIgnore
    @GetMapping("/getCapacityBoxStatus")
    @ApiOperation(value = "/capacity/getCapacityBoxStatus - 获版餐车柜子状态", httpMethod = "GET")
    public R<KxCapacityVo> getCapacityBoxStatus(@NotNull(message = "车辆编码不能为空") String capacityId) {
        return R.ok(iKxCapacityService.getCapacityBoxStatus(capacityId));
    }

    /**
     * 创建餐车柜格子订单
     */
    @SaIgnore
    @GetMapping("/createCapacityBoxOrder")
    @ApiOperation(value = "/capacity/createCapacityBoxOrder - 旧版创建餐车柜格子订单 - 作废", httpMethod = "GET")
    public PageData createCapacityBoxOrder(@NotNull(message = "车辆编码不能为空") String capacityId, @NotNull(message = "车厢索引不能为空") Integer boxIndex) {
        return iKxCapacityService.createCapacityBoxOrder(capacityId, boxIndex);
    }

    /**
     * 打开餐车柜格子
     */
    @SaIgnore
    @GetMapping("/openCapacityBox")
    @ApiOperation(value = "/capacity/openCapacityBox - 旧版打开餐车柜格子 - 作废", httpMethod = "GET")
    public R<Boolean> openCapacityBox(@NotNull(message = "车辆编码不能为空") String capacityId, @NotNull(message = "车厢索引不能为空") Integer boxIndex) {
        Boolean ret = iKxCapacityService.openCapacityBox(capacityId, boxIndex);
        return  R.ok(ret);
    }

    /**
     * 餐车柜格子订单支付成功通知
     */
    @SaIgnore
    @ResponseBody
    @RequestMapping(value="/abc_pay_notify", method = RequestMethod.POST)
    @ApiOperation(value = "/capacity/abc_pay_notify - 旧版餐车柜格子订单支付成功通知 - 作废", httpMethod = "POST")
    public void appAbcPayNotify(){
        log.info("abc_pay_notify start");
        HttpServletRequest request = getRequest();
        HttpServletResponse response = getResponse();
        try {
            String msg = request.getParameter("MSG");
            log.info("abc_pay_notify appAbcPayNotify msg: {}",msg);
            String ret = iKxCapacityService.appAbcPayNotify(msg);
            if(StringUtils.isNotEmpty(ret)) {
                response.sendRedirect("https://app.mosunshine.com/pages/boxBuy/boxBuy?id="+ret);
            }else {
                response.sendRedirect("https://app.mosunshine.com/pages/boxBuy/boxBuy");
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
    }

}
